package com.tungse.callable_test;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class Callable_Test {
    public static void main(String[] args) throws Exception {
        //FutureTask实现了Runnable接口，所以可以放到线程池中执行
        //FutureTask类实现了RunnableFuture接口，而RunnnableFuture接口继承了Runnable和Future接口，
        //所以它既可以作为Runnable被线程执行，又可以作为Future得到Callable的返回值。
        FutureTask<String> futureTask = new FutureTask<>(new CallableTest());
        new Thread(futureTask).start();
        System.out.println(futureTask.get());

        System.out.println("main end");
    }
}


class CallableTest implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println("CallableTest call");
        Thread.sleep(10000);
        return "你好";
    }
}